﻿using Microsoft.Win32;
using SIIE_ERP.Codigo;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Elysium.Demo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {   
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            bool _HasNetFramework = HasNetFramework();

            try
            {
                OleDbConnection myConnection = new OleDbConnection(SrvDB.GetDefaultConnection());
                myConnection.Open();
                
                OleDbCommand command = new OleDbCommand("SELECT * FROM dbo_Cat_Empresa", myConnection);
                OleDbDataAdapter da = new OleDbDataAdapter(command);
                DataSet dset = new DataSet();
                da.Fill(dset);

                if (dset.Tables[0].Rows.Count > 0)
                {
                    Login.Login l = new Login.Login();
                    l.Show();
                }
                else
                {
                    Elysium.Demo.Registro.WindowRegistro w = new Registro.WindowRegistro();
                    w.Show();
                }
            }
            catch (Exception)
            {
                Elysium.Demo.Registro.WindowRegistro w = new Registro.WindowRegistro();
                w.Show();
            }
            
        }

        private static bool IsOfficeInstalled()
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe");
            if (key != null)
            {
                key.Close();
            }
            return key != null;
        }

        private static bool HasNetFramework()
        {
            //.Net version: Apporach 1
            RegistryKey installed_versions = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full");
            if (installed_versions == null)
                return false;

            string MyVersion = "";
            try
            {
                MyVersion = installed_versions.GetValue("Release").ToString();
            }
            catch (Exception Ex)
            {
                return false;
            }

            if (!(MyVersion.Equals("378389") || MyVersion.Equals("378675") || MyVersion.Equals("378758")))
            {
                return false;
            }

            return true;
        }

        public bool DoHandle { get; set; }
        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            if (this.DoHandle)
            {
                //Handling the exception within the UnhandledExcpeiton handler.               

                //ENVIA EL CORREO DE ERROR
                SmtpClient mySmtpClient = new SmtpClient("smtp.uservers.net");
                MailMessage myMessage = new MailMessage();
                MailAddress fromAdd = new MailAddress("soporte@siie.com.mx");

                myMessage.Subject = "[SIE PyMES Desktop] Error";
                myMessage.IsBodyHtml = true;

                myMessage.Body = e.Exception.Message + "<br><br><br><br><br>" + e.Exception.StackTrace;

                myMessage.From = fromAdd;
                myMessage.To.Add("adolfol@siie.com.mx");
                myMessage.To.Add("carlos.nava@siie.com.mx");

                mySmtpClient.Port = 587;
                mySmtpClient.Credentials = new NetworkCredential("soporte@siie.com.mx", "Siie0214");
                mySmtpClient.Send(myMessage);

                e.Handled = true;
            }
            else
            {
                //If you do not set e.Handled to true, the application will close due to crash.
                //MessageBox.Show("Application is going to close! ", "Uncaught Exception");
                e.Handled = false;
            }
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;            

            //ENVIA EL CORREO DE ERROR
            SmtpClient mySmtpClient = new SmtpClient("smtp.uservers.net");
            MailMessage myMessage = new MailMessage();
            MailAddress fromAdd = new MailAddress("soporte@siie.com.mx");

            myMessage.Subject = "[SIE PyMES Desktop] Error";
            myMessage.IsBodyHtml = true;

            myMessage.Body = ex.Message + "<br><br><br><br><br>" + ex.StackTrace;

            myMessage.From = fromAdd;
            myMessage.To.Add("adolfol@siie.com.mx");

            mySmtpClient.Port = 587;
            mySmtpClient.Credentials = new NetworkCredential("soporte@siie.com.mx", "Siie0214");
            mySmtpClient.Send(myMessage);
        }
    }
}
